<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-left">
        <div class="hospital-bg">
          <div class="hospital-icon">
            <i class="el-icon hospital-symbol">
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
                <path d="M19 3H5C3.9 3 3 3.9 3 5V19C3 20.1 3.9 21 5 21H19C20.1 21 21 20.1 21 19V5C21 3.9 20.1 3 19 3M18 19H6C5.4 19 5 18.6 5 18V6C5 5.4 5.4 5 6 5H18C18.6 5 19 5.4 19 6V18C19 18.6 18.6 19 18 19M17 12H13V8H11V12H7V14H11V18H13V14H17V12Z"/>
              </svg>
            </i>
          </div>
          <h2 class="hospital-slogan">让医疗服务更有价值</h2>
          <p class="hospital-text">助医疗机构提升核心竞争力</p>
        </div>
        <div class="overlay"></div>
      </div>
      <div class="login-right">
        <div class="login-form">
          <div class="login-header">
            <div class="logo-container">
              <img src="@/assets/logo.jpg" alt="医院图片" class="hospital-image" />
            </div>
            <h1 class="system-name">HCRM系统</h1>
          </div>
          
          <el-form :model="loginForm" :rules="rules" ref="loginFormRef" class="form">
            <el-form-item prop="username">
              <el-input 
                v-model="loginForm.username" 
                placeholder="请输入用户名"
                :prefix-icon="User"
                size="large"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input 
                v-model="loginForm.password" 
                type="password" 
                placeholder="请输入密码"
                :prefix-icon="Lock"
                show-password
                size="large"
              />
            </el-form-item>
            
            <div class="remember-forgot">
              <el-checkbox v-model="rememberMe">记住密码</el-checkbox>
              <el-link type="primary" :underline="false" @click="handleForgetPassword">忘记密码？</el-link>
            </div>

            <el-button 
              type="primary" 
              :loading="loading" 
              class="login-button"
              size="large"
              @click="handleLogin"
            >
              登录系统
            </el-button>
          </el-form>
          <!-- <div>
            <el-link type="primary" :underline="false" @click="handleReg">没有账号？点击注册</el-link>
          </div> -->
          <div class="login-footer">
            <p>推荐使用Chrome浏览器访问系统</p>
            <p>如遇问题请联系管理员</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/LoginApi'
const router = useRouter()
const route = useRoute()
const loginFormRef = ref(null)
const loading = ref(false)
const rememberMe = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3-20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在6-20个字符之间', trigger: 'blur' }
  ]
}
// 登录
const handleLogin = () => {
  if (!loginFormRef.value) return
  loginFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      login(loginForm).then(res => {
        console.log(res)
        if (res.code === 200) {
          loading.value = false
          localStorage.setItem('token', res.access_token);
          localStorage.setItem('refresh_token', res.refresh_token);
          localStorage.setItem('username', loginForm.username)
          router.push('/')
        }
      })
    }
  })
}
// 注册
const handleReg = () => {
  router.push('/reg')
}

const handleForgetPassword = () => {
  router.push('/forgetPassword')
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  background: linear-gradient(135deg, #e6f3ff 0%, #ffffff 100%);
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-content {
  width: 1000px;
  height: 600px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  display: flex;
  overflow: hidden;
}

.login-left {
  flex: 1;
  position: relative;
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  overflow: hidden;
}

.hospital-bg {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  position: relative;
  z-index: 2;
}

.hospital-icon {
  width: 120px;
  height: 120px;
  margin-bottom: 24px;
}

.hospital-symbol {
  font-size: 120px;
  color: rgba(255, 255, 255, 0.9);
}

.hospital-slogan {
  font-size: 36px;
  font-weight: 600;
  margin-bottom: 16px;
  letter-spacing: 4px;
}

.hospital-text {
  font-size: 18px;
  opacity: 0.9;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.1'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
  opacity: 0.1;
}

.logo-container {
  width: 120px;
  height: 120px;
  margin: 0 auto 16px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.login-right {
  width: 460px;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.system-name {
  font-size: 28px;
  color: #1a1a1a;
  margin-bottom: 8px;
  font-weight: 600;
}

.system-desc {
  color: #666;
  font-size: 16px;
}

.form {
  margin-bottom: 20px;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
}

.login-footer {
  text-align: center;
  color: #999;
  font-size: 13px;
  margin-top: 40px;
  line-height: 1.6;
}

:deep(.el-input__wrapper) {
  padding: 4px 11px;
}

:deep(.el-input__inner) {
  height: 40px;
}
</style>

